package zero.tech.games.gate.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import zero.tech.core.data.Resp;
import zero.tech.core.data.SimpleUser;
import zero.tech.games.common.Code;
import zero.tech.games.entity.games.Club;
import zero.tech.games.entity.games.ClubMember;
import zero.tech.games.gate.hot.ClubUtil;
import zero.tech.games.gate.service.api.IClubService;

@RestController
public class ClubController extends BaseController{
	@Autowired
	private IClubService clubService;
	@Autowired
	private ClubUtil clubUtil;
	
	@RequestMapping("/club-create")
	public Resp clubCreate(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		SimpleUser simpleUser=super.getLoginUser(request);
		String club_name=(String) map.get("club_name");
		return clubService.clubCreate(simpleUser,club_name);
	}
	
	@RequestMapping("/club-list")
	public Resp clubList(HttpServletRequest request) {
		SimpleUser simpleUser=super.getLoginUser(request);
		return clubService.clubList(simpleUser);
	}
	
	
	
	@RequestMapping("/club-detail")
	public Resp clubDetail(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		int club_id=Integer.valueOf(map.get("club_id").toString());
		SimpleUser simpleUser=super.getLoginUser(request);
		int uid=simpleUser.getUid();
		ClubMember clubMember=clubUtil.getClubMember(uid,club_id);
		if(clubMember==null) {
			return Resp.build(Code.PARAM_ERROR, "不属于该茶馆");
		}
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		return clubService.clubDetail(simpleUser,club,clubMember);
	}
	
	
	@RequestMapping("/club-rooms")
	public Resp clubRooms(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		int club_id=Integer.valueOf(map.get("club_id").toString());
		int game_id=Integer.valueOf(map.get("game_id").toString());
		int rule_id=Integer.valueOf(map.get("rule_id").toString());
		int from=Integer.valueOf(map.get("from").toString());
		int to=Integer.valueOf(map.get("to").toString());
		SimpleUser simpleUser=super.getLoginUser(request);
		int uid=simpleUser.getUid();
		ClubMember clubMember=clubUtil.getClubMember(uid,club_id);
		if(clubMember==null) {
			return Resp.build(Code.PARAM_ERROR, "不属于该茶馆");
		}
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "俱乐部不存在");
		}
		return clubService.clubRooms(simpleUser,club,game_id,rule_id,from,to);
	}
}
